package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.admin.mall.vo.CategoryVo;
import com.cskaoyan.bean.admin.mall.vo.ChildrenEntity;
import com.cskaoyan.bean.admin.mall.vo.L1Vo;
import com.cskaoyan.bean.pojo.Category;
import com.cskaoyan.bean.pojo.CategoryExample;
import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.service.admin.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Name : CategoryServiceImpl.java
 * @Time : 2021/8/11 21:46
 * @Author : Xfeng
 * @Software : IntelliJ IDEA
 */
@Transactional
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<CategoryVo> list() {
        CategoryExample example1 = new CategoryExample();
        CategoryExample.Criteria criteria = example1.createCriteria().andPidEqualTo(0).andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(example1);
        List<CategoryVo> categoryVos = new ArrayList<>();
        for (Category category : categories) {
            Integer id = category.getId();
            CategoryExample example2 = new CategoryExample();
            CategoryExample.Criteria criteria2 = example2.createCriteria().andPidEqualTo(id).andDeletedEqualTo(false);
            List<Category> children = categoryMapper.selectByExample(example2);
            ArrayList<ChildrenEntity> childrenEntities = new ArrayList<>();
            for (Category child : children) {
                ChildrenEntity childrenEntity = new ChildrenEntity(child.getPicUrl(), child.getKeywords(), child.getLevel(), child.getName(), child.getId(), child.getIconUrl(), child.getDesc());
                childrenEntities.add(childrenEntity);
            }
            CategoryVo categoryVo = new CategoryVo(category.getPicUrl(), category.getKeywords(), category.getLevel(), childrenEntities, category.getName(), category.getId(), category.getIconUrl(), category.getDesc(),null,null,null);
            categoryVos.add(categoryVo);
        }
        return categoryVos;
    }

    @Override
    public List<L1Vo> l1() {
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria().andLevelEqualTo("L1").andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(example);
        List<L1Vo> l1Vos = new ArrayList<>();
        for (Category category : categories) {
            L1Vo l1Vo = new L1Vo(category.getName(), category.getId());
            l1Vos.add(l1Vo);
        }
        return l1Vos;
    }

    @Override
    public void delete(CategoryVo categoryVo) {
        Integer id = categoryVo.getId();
        Category category = new Category();
        category.setId(id);
        category.setDeleted(true);
        categoryMapper.updateByPrimaryKeySelective(category);
        if (categoryVo.getChildren() != null) {//说明是一级目录
            CategoryExample example = new CategoryExample();
            CategoryExample.Criteria criteria = example.createCriteria().andPidEqualTo(id).andDeletedEqualTo(false);
            List<Category> categories = categoryMapper.selectByExample(example);
            for (Category category1 : categories) {
                category1.setDeleted(true);
                categoryMapper.updateByPrimaryKeySelective(category1);
            }
        }
    }

    @Override
    public CategoryVo create(CategoryVo categoryVo) {
        Date date = new Date();
        Category category = new Category(null, categoryVo.getName(), categoryVo.getKeywords(), categoryVo.getDesc(), categoryVo.getPid(), categoryVo.getIconUrl(), categoryVo.getPicUrl(), categoryVo.getLevel(), null, date, date, false);
        categoryMapper.insertSelective(category);
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        categoryVo.setUpdateTime(format);
        categoryVo.setAddTime(format);
        categoryVo.setId(category.getId());
        return categoryVo;
    }

    @Override
    public void update(CategoryVo categoryVo) {
        Date date = new Date();
        Category category = new Category(categoryVo.getId(), categoryVo.getName(), categoryVo.getKeywords(), categoryVo.getDesc(), categoryVo.getPid(), categoryVo.getIconUrl(), categoryVo.getPicUrl(), categoryVo.getLevel(), null, null, date, null);
        categoryMapper.updateByPrimaryKeySelective(category);
    }
}
